﻿using Microsoft.Xaml.Behaviors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using WpfBehaviorDemo.Models;

namespace WpfBehaviorDemo.Behaviors
{
    public class CanvasDragBehavior : Behavior<Canvas>
    {

        protected override void OnAttached()
        {
            this.AssociatedObject.AllowDrop = true;

            this.AssociatedObject.DragEnter += AssociatedObject_DragEnter;//拖入
            this.AssociatedObject.DragLeave += AssociatedObject_DragLeave;//脱离
            this.AssociatedObject.DragOver += AssociatedObject_DragOver;//悬于上方
            this.AssociatedObject.Drop += AssociatedObject_Drop;//落下

            base.OnAttached();
        }

        private void AssociatedObject_Drop(object sender, System.Windows.DragEventArgs e)
        {
            var data = e.Data.GetData(typeof(ShapeModel)) as ShapeModel;
            var pos = e.GetPosition(this.AssociatedObject);//获取鼠标在指定容器中相对于左上角的坐标。

            if (data != null)
            {
                UIElement element = null;
                switch (data.ShapeType)
                {
                    case ShapeType.Ellipse:
                        element = new Ellipse() { Width = data.Size.Width, Height = data.Size.Height, Fill = data.Fill };
                        this.AssociatedObject.Children.Add(element);

                        break;
                    case ShapeType.Rectangle:
                        element = new Rectangle() { Width = data.Size.Width, Height = data.Size.Height, Fill = data.Fill };
                        this.AssociatedObject.Children.Add(element);
                        break;
                    case ShapeType.Triangle:
                        element = new Path()
                        {
                            Data = System.Windows.Media.Geometry.Parse
                            ($"M0,0 L{data.Size.Width},0 L{data.Size.Width / 2},{data.Size.Height} Z"),
                            Fill = data.Fill
                        };//没有三角，使用path绘制
                        this.AssociatedObject.Children.Add(element);
                        break;
                    default:
                        break;
                }

                //将指定元素重设位置，pos.X 和 pos.Y 分别是鼠标相对于 Canvas 左边缘和上边缘的坐标值 
                //这样写其实还是有点问题，因为不同形状可能求坐标的方式不同
                Canvas.SetLeft(element, pos.X - data.Size.Width / 2);
                Canvas.SetTop(element, pos.Y - data.Size.Height / 2);

                ////为元素添加行为
                //var behaviors = Interaction.GetBehaviors(element);
                //behaviors.Add(new CanvasEllementMoveBehavior());
            }
        }

        private void AssociatedObject_DragOver(object sender, System.Windows.DragEventArgs e)
        {
            //throw new NotImplementedException();
        }

        private void AssociatedObject_DragLeave(object sender, System.Windows.DragEventArgs e)
        {
            //throw new NotImplementedException();
        }

        private void AssociatedObject_DragEnter(object sender, System.Windows.DragEventArgs e)
        {
            //throw new NotImplementedException();
        }

        protected override void OnDetaching()
        {
            this.AssociatedObject.DragEnter -= AssociatedObject_DragEnter;//拖入
            this.AssociatedObject.DragLeave -= AssociatedObject_DragLeave;//脱离
            this.AssociatedObject.DragOver -= AssociatedObject_DragOver;//悬于上方
            this.AssociatedObject.Drop -= AssociatedObject_Drop;//落下

            base.OnDetaching();
        }

    }
}
